package org.example.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * FanoutExchange 模式
 *
 *@ClassName: FanoutRabbitConfig
 *@Author: CJ
 *@Date: 2021-9-24 10:45
 */
@Configuration
public class FanoutRabbitConfig {

    /*创建三个队列*/
    @Bean
    public Queue queueA() {
        return new Queue("fanout.A", true);
    }

    @Bean
    public Queue queueB() {
        return new Queue("fanout.B", true);
    }

    @Bean
    public Queue queueC() {
        return new Queue("fanout.C", true);
    }

    /*创建FanoutExchange交换机*/
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange", true, false);
    }

    /*将三个队列都绑定在交换机fanoutExchange上，因为是fanoutExchange, 路由键无需配置,配置也不起作用*/
    @Bean
    public Binding bindingExchangeA(){
        return BindingBuilder.bind(queueA()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingExchangeB(){
        return BindingBuilder.bind(queueB()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingExchangeC(){
        return BindingBuilder.bind(queueC()).to(fanoutExchange());
    }
}
